import titleBar from '@/components/title_bar/index.vue'
import {Copy} from '@/utils/object.js'
import {id} from '@/utils/_.js'
import {getuserUid} from '@/utils/auth.js'
import {mapMutations} from 'vuex'
export default {
    components: {
        titleBar,
    },
    created(){
        let referer = id(this.$referer)
        // xx师中心点击进来的
        // let isRefererCenter = Boolean(referer.path.split('?')[0].includes('designer/personalCenter'))
        let isRefererCenter = Boolean(this.$route.query?.ismine)
        this.isRefererCenter = isRefererCenter
        if(isRefererCenter){
            console.log('center')
        }
    },
    data(){
        return {
            addresss: [],
            loading: false,
            finished: false,
            isLoading: false,
            pageSize: 7,
            pageNum: 0,
            oladParams: {},
            isRefererCenter: false
        }
    },
    methods: {
        ...mapMutations([
            'UPDATE',
        ]),
        // 编辑
        edit(id){
            this.$router.replace(`/performance/address-manage?edit=1&id=${id}`)
        },
        // 编辑发票地址
        toGoFaPiaoEdit(row){
            console.log(row)
            // {
            //     userName: row.userName,
            //     phone: row.phone,
            //     provinceUid: row.provinceUid,
            //     cityUid: row.cityUid,
            //     areaUid: row.areaUid,
            //     detailsAddress: row.detailsAddress,
            // }
            this.UPDATE(['editAddrerss', row])
            this.$router.replace({
                name:'designer-addresEditing',
                params: {
                    fromInvoice: 'oldInvoice',
                    isAddress: '1'
                }
            })
        },
        async selectItem(row){
            // console.log('---------row----------')
            // console.log(row)
            let referer = id(this.$referer)
            // 如果是设计师中心返回过来的，点击编辑，如果是下单点击中间的返回上一页并传参数
            if(this.isRefererCenter){
                row.addressType === 2 && this.edit(row.uid)
                row.addressType === 1 && this.toGoFaPiaoEdit(row)
                return
            }
            console.log(id)
            console.log(referer)
            let response = await HTTP('app/buser/addressCheck', {
                addressUid: row.uid,
                sUserUid: getuserUid(),
                bUserUid: referer.query.userUid,
            })
            
            if(response.code !== 200){
                this.$dialog.confirm({
                    title: '提示',
                    message: '该地址超出表现师的服务范围',
                    confirmButtonText: '重选地址',
                    cancelButtonText: '重新选人',
                }).then(() => {
                    //
                }).catch(() => {
                    this.$router.replace('/')
                })
                return
            }
            this.$router.replace({
                path: referer.path,
                query: {...referer.query, addressid: row.id, addressuid: row.uid}
            })
        },
        onedit(row){
            row.addressType === 2 && this.edit(row.uid)
            row.addressType === 1 && this.toGoFaPiaoEdit()
            if(!row.addressType){
                this.$toast('后台异常')
            }
        },
        onRefresh(){
            this.isLoading = true
            this.requestList(this.oladParams, () => {
                // 太慢了不差半秒，太快需要延时（因为用户在滑动的时候有心理准备，速度太快了反而担心这是假的）
                setTimeout(() => {
                    this.isLoading = false
                }, 500)
            })
        },
        onLoad(){
            this.loading = true
            // console.log(1)
            // setTimeout(() => {
            //     this.loading = false
            // }, 1e3)
            // return
            this.pageNum += 1
            let params = {
                userUid: getuserUid(),
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                addressType: this.isRefererCenter ? 3 : 2,
            }
            console.log(params)
            setTimeout(() => {
                this.requestList(params, (data) => {
                    this.loading = false
                    this.addresss.push(...data)
                    console.log(data)
                    if(data && data.length === 0){
                        this.finished = true
                    }
                })
            }, 0)
        },
        async requestList(params, cb){
            this.oladParams = params
            try {
                let responseList = await HTTP('app/userAddress/userAddressList', params)                
                if(responseList.code !== 200){
                    this.$toast(responseList.msg || '服务器异常')
                    return
                }
                let list = responseList.data.list.map(e => {
                    return {
                        ...e,
                        name: e.userName,
                        phone: e.phone,
                        address: `${e.address}  ${e.addressInfo}`
                    }
                })
                cb && cb(list)
            } catch (error) {
                this.$toast('数据开小差了')
                console.log(error)
                cb && cb()
            }
            
        },
        add(){
            this.$router.replace('/performance/address-manage')
        },
    }
}